﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SimulatingEnvironment.cs" company="pwasiewicz">
//   pwasiewicz
// </copyright>
// <summary>
//   Defines the SimulatingEnvironment type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace LogicSystem
{
    using System.Collections.Generic;
    using LogicSystem.Helpers;
    using LogicSystem.Interfaces;
    using LogicSystem.LogicGates;

    /// <summary>The simulating environment.</summary>
    internal class SimulatingEnvironment : ISimulatingEnvironment<int, LogicGate>
    {
        /// <summary>Initializes a new instance of the <see cref="SimulatingEnvironment"/> class.</summary>
        internal SimulatingEnvironment()
        {
            this.Heap = new MinHeap<int, LogicGate>();
        }

        /// <summary>
        /// Gets or sets total simulating time.
        /// </summary>
        public int TotalTime { get; set; }

        /// <summary>
        /// Gets or sets heap structure.
        /// </summary>
        private MinHeap<int, LogicGate> Heap { get; set; }

        /// <summary>
        /// Resets environment.
        /// </summary>
        public void Reset()
        {
            this.TotalTime = 0;
            this.Heap.Clear();
        }

        /// <summary>
        /// Pushes into heap key-value.
        /// </summary>
        /// <param name="key">The time.</param>
        /// <param name="value">The gate.</param>
        public void PushIntoHeap(int key, LogicGate value)
        {
            this.Heap.Insert(key, value); 
        }

        /// <summary>
        /// Pushes into heap key-value.
        /// </summary>
        /// <param name="items">Key-value pair collection.</param>
        public void PushIntoHeap(IEnumerable<KeyValuePair<int, LogicGate>> items)
        {
            this.Heap.Insert(items);
        }

        /// <summary>Gets top element.</summary>
        /// <returns>The <see cref="KeyValuePair"/>.</returns>
        public KeyValuePair<int, LogicGate> Top()
        {
            return this.Heap.Top();
        }

        /// <summary>
        /// Pops from heap.
        /// </summary>
        /// <returns>The pair.</returns>
        public KeyValuePair<int, LogicGate> PopFromHeap()
        {
            return this.Heap.Remove();
        }

        /// <summary>
        /// Determines whether this heap is empty.
        /// </summary>
        /// <returns>
        ///   <c>true</c> if this heap is empty; otherwise, <c>false</c>.
        /// </returns>
        public bool IsEmpty()
        {
            return this.Heap.IsEmpty();
        }
    }
}
